package com.abc.controller;

import com.abc.entity.Product;
import com.abc.entity.User;
import com.abc.service.ProductService;
import com.abc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.nio.channels.SeekableByteChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private UserService userService;

    // 根据id查询
    @GetMapping("/find") // 这里为什么不能用post
    public String find(String id, Model model) {
        Product product = productService.find(id);
        model.addAttribute("product", product);
        return "/ims/admin/updateProduct";
    }

    // 查询所有数据
    @GetMapping("findAll")
    public String findAll(Model model, @RequestParam("type") String type) {
        List<Product> products = productService.findAll();
        model.addAttribute("products", products);
        if ("admin".equals(type)) {
            System.out.println("进入管理员的页面");
            return "/ims/admin/productlist";
        } else if ("user".equals(type)) { // "user".equals(type)
            System.out.println("进入普通用户的页面");
            return "/ims/user/productlist";
        } else { // "visitor".equals(type)
            System.out.println("进入游客登录的页面");
            return "/ims/visitor/productlist";
        }
    }

    // 删除某条影像制品
    @GetMapping("/delete")
    public String delete(String id) {
        productService.delete(Integer.parseInt(id));
        return "redirect:/product/findAll?type=admin";
    }

    // 保存员工
    @PostMapping("/save")
    public String save(Product product) {
        productService.save(product);
        return "redirect:/product/findAll?type=admin"; // 斜杠不能省略
    }

    // 更新产品信息
    @PostMapping("/update")
    public String update(Product product) {
        System.out.println("update " + product);
        productService.update(product);
        return "redirect:/product/findAll?type=admin"; // 斜杠不能省略
    }

    // 更改是否可租
    @GetMapping("/changeCanRent")
    public String changeCanRent(String id) {
        Product product = productService.find(id);
        if ("0".equals(product.getCanRent())) {
            product.setCanRent("1");
        } else if ("1".equals(product.getCanRent())) {
            product.setCanRent("0");
        }
        productService.update(product);
        return "redirect:/product/findAll?type=admin";
    }

    // 修改 与 还书
    @GetMapping("/changeRentType")
    public String changeRentType(String id) {
        Product product = productService.find(id);
        if ("0".equals(product.getRentType())) { // 还书
            product.setRentType("1");
            product.setOrderTime(null); // 清空预约时间
            product.setEndTime(new Date()); // 设置还书时间
        } else if ("1".equals(product.getRentType())) { // 修改
//            product.setRentType("0");
            product.setOrderTime(null);
            product.setEndTime(null);
            product.setUserid(null);
            product.setUsername(null);
        }
        System.out.println(product);
        productService.update(product);
        return "redirect:/product/findAll?type=admin";
    }

    // 去预约页面
    @GetMapping("/toOrder")
    public String toOrder(String id, Model model) {
        Product product = productService.find(id);
        model.addAttribute("product", product);
        return "/ims/user/order";
    }

    // 用户 执行 预约 操作
    @PostMapping("/order")
    public String order(Product product, HttpSession session) {
        User user = (User) session.getAttribute("user"); // 当前用户
        Date orderTime = product.getOrderTime(); // 界面上写的时间

        product = productService.find(product.getId() + "");
        product.setOrderTime(orderTime); // 设置预约时间
        product.setUserid(user.getId()); // 设置借给用户
        product.setUsername(userService.find(user.getId()).getUsername());  // 设置借给用户名
        product.setRentType("0"); // 外借中
        productService.update(product);
        return "redirect:/product/findAll?type=user";
    }

}
